package net.htjs.juc.chapter04;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class DemoSynchronizedBlock {


    /**
     * 线程1运行完,线程2再运行，Synchronized包裹的代码具有原子性，不可分割
     * @param args
     */
    public static void main(String[] args) {
        Number num = new Number();
        new Thread(() -> {
            log.info("begin");
            num.a();
        }).start();

        new Thread(() -> {
            log.info("begin");
            num.b();
        }).start();

        new Thread(() -> {
            log.info("begin");
            num.c();
        }).start();
    }

    static class Number {

        public synchronized void a() {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("1");
        }

        public synchronized void b() {
            log.info("2");
        }

        public void c() {
            log.info("3");
        }

    }
}
